package com.fll.fangyicommon.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author fll
 * @create 2023/2/24
 */
public class TimeUtil {

    /**
     * date日期转为 yyyy-MM-dd HH:mm:ss 字符串
     * @param date
     * @return
     */
    public static String getString(Date date){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    /**
     * yyyy-MM-dd HH:mm:ss 字符串转为date
     * @param str
     * @return
     */
    public static Date getDateByString(String str){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return df.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 计算两个字符串类型的yyyy-MM-dd HH:mm:ss日期之间，相差多少小时
     * @param time1
     * @param time2
     * @return
     */
    public static int getTime(String time1, String time2){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = format.parse(time1);
            date2 = format.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar ca1 = Calendar.getInstance();
        Calendar ca2 = Calendar.getInstance();
        ca1.setTime(date1);
        ca2.setTime(date2);
        //得出两个日期直接相差的时间戳数字（时间戳单位为毫秒）
        long time = (ca2.getTimeInMillis() - ca1.getTimeInMillis());
        //整理出返回分钟
        int t = (int) (time / (1000 * 60));
        return t;
    }

    /**
     * 获取两个string日期之间具体相差的天数、小时、分钟、秒数
      * @param time1
     * @param time2
     * @return
     */
    public static int getTime2(String time1, String time2){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //将String转换为localDateTime类型
        LocalDateTime localDateTime1 = LocalDateTime.parse(time1,dateTimeFormatter);
        LocalDateTime localDateTime2 = LocalDateTime.parse(time2,dateTimeFormatter);
        //LocalDateTime自带时间差计算（开始时间，结束时间）
        Duration duration = Duration.between(localDateTime1,localDateTime2);
        //两个时间之间，总共差的天/时/分/秒，
        long day = duration.toDays();
        long hours = duration.toHours();
        long minute = duration.toMinutes();
        //toMillis()获取的是毫秒所以除以1000换算成秒
        long second = duration.toMillis()/1000;
        //两个时间之间差了几天又几时又几分又几秒
        long Hours = hours - day * 24;
        long Minute = (minute - day * 24 * 60) - (Hours * 60);
        long Second = (second - day * 24 * 60 * 60) - (Hours * 60 * 60) - (Minute * 60);
        return (int)day;
    }

    /**
     * 两个String时间之间间隔多少天
     * eg：     String time1 = "2023-04-01 23:59:59";
     *          String time2 = "2023-04-02 00:00:00";
     *         哪怕两个日期只相差1秒，也是间隔1天
     * @param time1
     * @param time2
     * @return
     */
    public static int betweenDays(String time1, String time2) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar_a = Calendar.getInstance(); //获取日历对象
        Calendar calendar_b = Calendar.getInstance();

        Date date_a = null;
        Date date_b = null;

        try {
            date_a = simpleDateFormat.parse(time1);//字符串转Date
            date_b = simpleDateFormat.parse(time2);
            calendar_a.setTime(date_a);// 设置日历
            calendar_b.setTime(date_b);
        } catch (ParseException e) {
            e.printStackTrace();//格式化异常
        }

        long time_a = calendar_a.getTimeInMillis();
        long time_b = calendar_b.getTimeInMillis();

        int day = (int)(time_b - time_a) / (1000 * 3600 * 24);//计算相差天数
        return day;
    }

    /**
     * String日期做加减
     * @param time
     * @param day
     * @return
     */
    public static String dateStringSub(String time, int day){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date classDate = null; //把字符串转化成指定格式的日期
        try {
            classDate = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance(); //使用Calendar日历类对日期进行加减
        calendar.setTime(classDate);
        calendar.add(Calendar.DAY_OF_MONTH, - day);
        classDate = calendar.getTime(); //获取加减以后的Date类型日期
        return format.format(classDate);
    }

}
